package com.unionftech.content_user.ui.dialog

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.WindowManager
import android.widget.Button
import android.widget.TextView
import com.unionftech.content_user.R

class UFLogoutDialog(context: Context) : Dialog(context) {
    private val mContext = context
    private val mTvLogoutTitle: TextView by lazy { findViewById(R.id.tv_logout_title) }
    private val mBtnDialogCancel: Button by lazy { findViewById(R.id.btn_dialog_cancel) }
    private val mBtnDialogConfirm: Button by lazy { findViewById(R.id.btn_dialog_confirm) }
    private var mTitleText: String = ""
    private var mCancelText: String = ""
    private var mConfirmText: String = ""
    private var mCallBack: ((Boolean) -> Unit)? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val contentView =
            LayoutInflater.from(mContext).inflate(R.layout.layout_uf_logout_dialog, null)
        setContentView(contentView)
        setCancelable(true)
        setCanceledOnTouchOutside(true)
        setListener()
    }

    private fun setListener() {
        mBtnDialogCancel.setOnClickListener {
            mCallBack?.invoke(false)
            dismiss()
        }
        mBtnDialogConfirm.setOnClickListener {
            mCallBack?.invoke(true)
            dismiss()
        }
    }

    fun setCallBack(callback: (Boolean) -> Unit): UFLogoutDialog {
        this.mCallBack = callback
        return this
    }

    fun setTitle(title: String): UFLogoutDialog {
        mTitleText = title
        return this
    }

    fun setConfirmText(text: String): UFLogoutDialog {
        mConfirmText = text
        return this
    }

    fun setCancelText(text: String): UFLogoutDialog {
        mCancelText = text
        return this
    }

    private fun setView() {
        mTvLogoutTitle.text = mTitleText
        mBtnDialogCancel.text = mCancelText
        mBtnDialogConfirm.text = mConfirmText
    }

    override fun onStart() {
        super.onStart()
        window?.decorView?.setPadding(0, 0, 0, 0)
        window?.attributes?.gravity = Gravity.CENTER
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        window?.setLayout(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.WRAP_CONTENT
        )
        setView()
    }

    override fun show() {
        super.show()
    }
}